#!/bin/bash
#
# File: common.sh
# Purpose: 通用方法
# Note: 
#


. ./defaults.sh
test -f ./local.sh && . ./local.sh

confirmed_dir=./$config_dir
confirmed_tmp_dir=./$config_tmp_dir
confirmed_doing_dir=./$config_doing_dir

mkdir -p $confirmed_dir
mkdir -p $confirmed_tmp_dir
mkdir -p $confirmed_doing_dir

TUNNEL_NAME=
REMOTE_USER=
REMOTE_IP=
REMOTE_PORT=
LOCAL_IP=
LOCAL_PORT=
INSTALLDIR=$(cd "$(dirname "$0")"; pwd);
CHECK_PORT_SCRIPT=$INSTALLDIR/check-port.sh
CHECK_IP_SCRIPT=$INSTALLDIR/check-ip.sh
CHECK_NUMBER_SCRIPT=$INSTALLDIR/check-number.sh
CHECK_LETTER_SCRIPT=$INSTALLDIR/check-letter.sh
KENGEN_SCRIPT=$INSTALLDIR/ssh-keygen.sh

exitcode=1
LINE_NUMBER=0


is_confirmed_tunnelsite() {
    file=$confirmed_dir/$1
    test -f $file && return 0 || return 1
}

is_tmp_confirmed_tunnelsite() {
    file=$confirmed_tmp_dir/$1
    test -f $file && return 0 || return 1
}

heading() {
    echo '[####]' $*
}

info() {
    echo '[INFO]' $@
}
result() {
    echo '[RESULT]' $@
}
warn() {
    echo '[WARN]' $@
}

require() {
    if ! type "$1" >/dev/null 2>/dev/null; then
        echo 'Fatal: program "'$1'" is either not in PATH or not installed. Exit.'
        exit 1
    fi
}

match_session() {
    screen -ls | grep -F .$1
}

input_tunnel_name() {
	TUNNEL_NAME_SUCCESS=1;
    while true
    do
        printf "Enter tunnel name:" $TUNNEL_NAME;
        read TUNNEL_NAME
        if [ -z "$TUNNEL_NAME" ] ; then
			continue;
		else
		    echo $TUNNEL_NAME | grep " " >/dev/null 2>&1
			if [ $? = 0 ];then
				warn 'The input string can not contain spaces.'
				continue;
			fi
        fi
        if test -x $CHECK_LETTER_SCRIPT ; then
            $CHECK_LETTER_SCRIPT $TUNNEL_NAME
            if [ $? -eq 0 ]; then
                TUNNEL_NAME_SUCCESS=0;
				exitcode=0;
                break;  
            fi
		else
			exit 1;
        fi			
    done
    return $TUNNEL_NAME_SUCCESS;
}

input_remote_user() {
	REMOTE_USER_SUCCESS=1;
    while true
    do
        printf "Enter tunnel remote login user:" $REMOTE_USER;
        read REMOTE_USER

		if [ -z "$REMOTE_USER" ] ; then
			continue;
		else
		    echo $TUNNEL_NAME | grep " " >/dev/null 2>&1
			if [ $? = 0 ];then
				warn 'The input string can not contain spaces.'
				continue;
			fi			
        fi
		
        if test -x $CHECK_LETTER_SCRIPT ; then
            $CHECK_LETTER_SCRIPT $REMOTE_USER
            if [ $? -eq 0 ]; then
                REMOTE_USER_SUCCESS=0;
				exitcode=0;
                break;  
            fi
		else
			exit 1;
        fi		
		
    done
    return $REMOTE_USER_SUCCESS;
}

input_remote_ip() {
	REMOTE_IP_SUCCESS=1;
    while true
    do
        printf "Enter tunnel remote ip:" $REMOTE_IP;
        read REMOTE_IP

		if [ -z "$REMOTE_IP" ] ; then
			continue;
        fi

        if test -x $CHECK_IP_SCRIPT ; then
            $CHECK_IP_SCRIPT $REMOTE_IP
            if [ $? -eq 0 ]; then
                REMOTE_IP_SUCCESS=0;
				exitcode=0;
                break;  
            fi
		else
			exit 1;
        fi
    done
    return $REMOTE_IP_SUCCESS;
}

input_local_ip() {
	LOCAL_IP_SUCCESS=1;
    while true
    do
        printf "Enter tunnel local ip:" $LOCAL_IP;
        read LOCAL_IP

		if [ -z "$LOCAL_IP" ] ; then
			continue;
        fi

        if test -x $CHECK_IP_SCRIPT ; then
            $CHECK_IP_SCRIPT $LOCAL_IP
            if [ $? -eq 0 ]; then
                LOCAL_IP_SUCCESS=0;
				exitcode=0;
                break;  
            fi
		else
			exit 1;
        fi
    done
    return $LOCAL_IP_SUCCESS;
}

input_remove_port () {

    REMOTE_PORT_SUCCESS=1;
    while true
    do 
        printf "Enter tunnel remote port:" $REMOTE_PORT;
        read REMOTE_PORT
		
		if [ -z "$REMOTE_PORT" ] ; then
            continue;
        fi
		if [ $REMOTE_PORT -gt 1024 ] && [ $REMOTE_PORT -lt 65535 ]; then		
			if test -x $CHECK_NUMVER_SCRIPT; then
				$CHECK_NUMBER_SCRIPT $REMOTE_PORT
				if [ $? -eq 0 ]; then
					REMOTE_PORT_SUCCESS=0;
					exitcode=0;
					break;      
				fi
			else
				echo 'check number file is not found.' 
				exit 1;
			fi
		else
		    echo 'The port number must be between 1024-65535.' 
		fi
    done 
    return $REMOTE_PORT_SUCCESS
	
}

input_local_port () {
    LOCAL_PORT_SUCCESS=1;
    while true
    do
        printf "Enter tunel local port:" $LOCAL_PORT;
        read LOCAL_PORT
		
		if [ -z "$LOCAL_PORT" ] ; then
            continue;
        fi
		if [ $LOCAL_PORT -gt 1024 ] && [ $LOCAL_PORT -lt 65535 ]; then
			if test -x $CHECK_NUMBER_SCRIPT; then
				$CHECK_NUMBER_SCRIPT $LOCAL_PORT
				if [ $? -eq 0 ]; then
					if test -x $CHECK_PORT_SCRIPT; then
						$CHECK_PORT_SCRIPT $LOCAL_PORT
						#检查端口占用情况，没有被占用
						if [ $? -ne 0 ]; then
							LOCAL_PORT_SUCCESS=0;
							exitcode=0;
							break;
						else
							LOCAL_PORT_SUCCESS=1; 
						fi
					fi
				else
					echo 'The input port is not a number.' 
				fi
			else
				echo 'check number file is not found.' 
				exit 1;
			fi
		else
			echo 'The port number must be between 1024-65535.' 		    
		fi

    done
    return $LOCAL_PORT_SUCCESS 
}

ssh_keygen() {
	KEY_GEN_SUCCESS=1
	if test -x $KENGEN_SCRIPT 
	then
		$KENGEN_SCRIPT;
		exitcode=$?;
		KEY_GEN_SUCCESS=$exitcode;
	else
		exitcode = 1;
	fi
	return $KEY_GEN_SUCCESS
}


queryOfLineNumber() {
	query_result=0;
	file=$confirmed_dir/$1;
	txt=$2;
	if test -f $file ; then
		lin=`(grep -n $txt= | awk '{print $1}' | cut -d : -f 1) < $file`;
		if [ ! $lin ]; then
			info 'query '$txt' is not found'
			query_result=0;
		else
			LINE_NUMBER=$lin;
			query_result=0;
		fi
	else
		warn $file' not exist'
		query_result=0;
	fi
	return $query_result;
}

delOfLineNumber() {
	tmpFile=$confirmed_dir/'tunnel.tmp';
	file=$confirmed_dir/$1;
	lin=$2;
	opt=`sed -e '${lin}d' < ${file} > ${tmpFile}`;
	if [ ! $opt ]; then
		rm -rf $tmpFile;
		return 0;
	else
		rm -rf $file;
		mv $tmpFile $file;
		return 0;
	fi
}

queryTunnelDoing() {
	for file_a in ${confirmed_doing_dir}/*; do 
		if test -f $file_a; then
			temp_file=`basename $file_a`
			if [ "$temp_file" = "$1" ];then
			    return 0;
			fi
			
		fi
	done
	return 1;
}
# eof
